<?php
protectScript('NewsModel.php');

class NewsModel{
	private static $_asmReadOnlyProps = array(
		'sTableName' => 'news',
		'sTablePrefix' => TABLE_PREFIX
	);

	private $_asmWritableProps = array(
		'iIdNews' => 0,
		'iDateNews' => 0,
		'sTitleNews' => '',
		'sTextNews' => ''
	);

	public function __construct($asmNewsInfo = null){
		$this->_asmWritableProps['iIdNews'] = (isset($asmNewsInfo['idNews'])) ? $asmNewsInfo['idNews'] : $this->_asmWritableProps['iIdNews'];
		$this->_asmWritableProps['iDateNews'] = (isset($asmNewsInfo['dateNews'])) ? $asmNewsInfo['dateNews'] : $this->_asmWritableProps['iDateNews'];
		$this->_asmWritableProps['sTitleNews'] = (isset($asmNewsInfo['titleNews'])) ? $asmNewsInfo['titleNews'] : $this->_asmWritableProps['sTitleNews'];
		$this->_asmWritableProps['sTextNews'] = (isset($asmNewsInfo['textNews'])) ? $asmNewsInfo['textNews'] : $this->_asmWritableProps['sTextNews'];
	}

	public function getNews($sWhereClause = null, $sLimitClause = null){
		$oMysqliInstance = AYSMysqli::getInstance();

		if(isset($sLimitClause)){
			$sLimit = $sLimitClause;
		}else{
			$sLimit = '';
		}
		if(isset($sWhereClause)){
			$sWhere = $sWhereClause;
		}else{
			$sWhere = '';
		}
		$query = sprintf(
			'SELECT idNews, dateNews, titleNews, textNews ' .
			"FROM %s $sWhere ORDER BY dateNews DESC $sLimit",
			self::$_asmReadOnlyProps['sTablePrefix'].self::$_asmReadOnlyProps['sTableName']
		);
		$rQueryResult = $oMysqliInstance->query($query);

		$aiasmNewsList = array();
		while($r = $rQueryResult->fetch_assoc()){
			$aiasmNewsList[] = $r;
		}
		return $aiasmNewsList;
	}

	public static function addNews($assNewsInfos){
		$oMysqliInstance = AYSMysqli::getInstance();
		$query = sprintf(
			"INSERT INTO %s (titleNews, textNews)".
			"VALUES ('%s', '%s')",
			self::$_asmReadOnlyProps['sTablePrefix'].self::$_asmReadOnlyProps['sTableName'],
			$oMysqliInstance->escape_string($assNewsInfos['titleNews']),
			$oMysqliInstance->escape_string($assNewsInfos['textNews'])
		);
		$oMysqliInstance->query($query);
	}

	public function editNews($assNewsInfos){
		$oMysqliInstance = AYSMysqli::getInstance();
		$query = sprintf(
			"UPDATE %s ".
			"SET titleNews = '%s', textNews = '%s' ".
			"WHERE idNews = %d",
			self::$_asmReadOnlyProps['sTablePrefix'].self::$_asmReadOnlyProps['sTableName'],
			$oMysqliInstance->escape_string($assNewsInfos['titleNews']),
			$oMysqliInstance->escape_string($assNewsInfos['textNews']),
			$assNewsInfos['idNews']
		);

		$oMysqliInstance->query($query);
	}

	public static function delNews($iIdNews){
		$oMysqliInstance = AYSMysqli::getInstance();
		$query = sprintf(
			"DELETE FROM %s ".
			"WHERE idNews = %d",
			self::$_asmReadOnlyProps['sTablePrefix'].self::$_asmReadOnlyProps['sTableName'],
			$iIdNews
		);
		$oMysqliInstance->query($query);
	}

	public function __get($sVariableName){
		if(isset($this->_asmReadOnlyProps[$sVariableName])){
			if(is_array($this->_asmReadOnlyProps[$sVariableName])){
				return clone new ArrayObject($this->_asmReadOnlyProps[$sVariableName]);
			}elseif(is_object($this->_asmReadOnlyProps[$sVariableName])){
				return clone $this->_asmReadOnlyProps[$sVariableName];
			}else{
				return $this->_asmReadOnlyProps[$sVariableName];
			}
		}elseif(!isset($this->_asmWritableProps[$sVariableName])){
			$this->_asmWritableProps[$sVariableName] = NULL;
		}

		return $this->_asmWritableProps[$sVariableName];
	}

	public function __set($sVariableName, $mValueVariable){
		if(isset($this->_asmReadOnlyProps[$sVariableName])){
			throw new SetReadOnlyPropsException(
				'Tried to set a read only property on the event object',
				ERROR,
				array(
					'scriptName' => 'NewsModel.php',
					'variableName' => $sVariableName
				)
			);
		}

		return $this->_asmWritableProps[$sVariableName] = $mValueVariable;
	}
}
